/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.data;

import genomemap.tagging.LinkingCloneStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author stewari1
 */
class PhysicalMapServiceHelper {

    /**
     *
     * @param probeIndex
     * @param order
     * @param binaryProfile
     * @param clones ordered in ascending order of their orders
     * @param linkCloneStrategy a strategy to choose one clone among aothers
     * @param linkedCloneList searching for clones barring this list
     * @return
     */
    static Clone getLinkingClone(int probeIndex1, int probeIndex2, int[][] binaryProfile,
                                 Clone[] clones, LinkingCloneStrategy linkCloneStrategy,
                                 List<Clone> linkedCloneList) {

        return findPrefTypeClone(getLinkingClones(probeIndex1, probeIndex2, binaryProfile, clones,
                linkedCloneList), linkCloneStrategy);
    }

    /**
     *
     * @param probeIndex1
     * @param probeIndex2
     * @param binaryProfile
     * @param clones
     * @param linkedCloneList searching for clones barring this list
     * @return
     */
    static List<Clone> getLinkingClones(int probeIndex1, int probeIndex2, int[][] binaryProfile,
            Clone[] clones, List<Clone> linkedCloneList) {

        List<Clone> linkedClones = new ArrayList<>();

        for (int i = 0; i < binaryProfile.length; i++) {

            if ((binaryProfile[i][probeIndex1] == 1) && (binaryProfile[i][probeIndex2] == 1)) {

                if (!linkedCloneList.contains(clones[i])) {
                    linkedClones.add(clones[i]);
                }
            }
        }

        return linkedClones;
    }

    static int getCorrespondingCloneIndex(Clone[] clones, Probe probe) {

        for (int i = 0; i < clones.length; i++) {

            if (clones[i].getLabel().equals(probe.getLabel())
                    && (clones[i].getChId() == probe.getChId())) {
                return clones[i].getOrder() - 1;
            }
        }

        return -1;
    }

    /**
     * Method description
     *
     * @param cloneList description
     * @param prefCloneStrategy description
     *
     * @return description
     */
    private static Clone findPrefTypeClone(List<Clone> cloneList,
            LinkingCloneStrategy prefCloneStrategy) {

        Clone result             = null;
        int[] prefCloneTypeOrder = prefCloneStrategy.getCloneTypePrefOrder();

        for (int i = 0; i < prefCloneTypeOrder.length; i++) {

            for (Clone clone : cloneList) {

                switch (prefCloneTypeOrder[i]) {

                    case Clone.S_CLONE :
                        if (clone.isSClone()) {
                            result = clone;
                        }

                        break;

                    case Clone.R_CLONE :
                        if (clone.isRClone()) {
                            result = clone;
                        }

                        break;

                    case Clone.A_CLONE :
                        if (clone.isAClone()) {
                            result = clone;
                        }
                }

                if (clone != null) {
                    break;
                }
            }

            if (result != null) {
                return result;
            }
        }

        return result;
    }
}
